﻿using ASmile.Com;
using ASmile.Com.MenuRole;
using ASmile.DBModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASmile.Win.DaBLL
{
    class MenuRoleBLL : BLLBase, IMenuRoleBLL
    {
        protected override string ControllerName => "SysManage/MenuRole";

        #region 角色维护

        /// <summary>
        /// 新增保存
        /// </summary>
        [WebApi("SysManage/Role", "Add")]
        public virtual ApiResult<string> RoleAdd(Sys_Role model)
        {
            return GetResult<string>(model);
        }
        /// <summary>
        /// 编辑保存
        /// </summary>
        [WebApi("SysManage/Role", "Update")]
        public virtual ApiResult<string> RoleEdit(Sys_Role model)
        {
            return GetResult<string>(model);
        }
        /// <summary>
        /// 删除
        /// </summary>
        [WebApi("SysManage/Role", "Del")]
        public ApiResult RoleDel(Sys_Role model)
        {
            return GetResult(model);
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        [WebApi("SysManage/Role", "GetList")]
        public ApiResultList<Sys_Role> RoleGetData()
        {
            return GetResultList<Sys_Role>(new ArgPageData());
        }

        #endregion

        #region 菜单树

        /// <summary>
        /// 菜单分类列表（非菜单的树形结构）
        /// </summary>
        public ApiResultList<Sys_RoleMenu> TreeGetData(string roleId)
        {
            return GetResultList<Sys_RoleMenu>(new ArgId(roleId));
        }

        public ApiResult<string> TreeAdd(Sys_RoleMenu m)
        {
            return GetResult<string>(m);
        }

        public ApiResult<string> TreeEdit(Sys_RoleMenu m)
        {
            return GetResult<string>(m);
        }

        public ApiResult TreeChangeParentId(Sys_RoleMenu m, string pId)
        {
            m.Tag = pId;
            return GetResult(m);
        }

        public ApiResult TreeChangeParentId(Sys_RoleMenu m)
        {
            return GetResult(m);
        }

        public ApiResult TreeDel(Sys_RoleMenu menu)
        {
            return GetResult(menu);
        }

        /// <summary>
        /// 交换排序
        /// </summary>
        public ApiResult SwapOrderNo(Sys_RoleMenu srcModel, Sys_RoleMenu tarModel)
        {
            ArgSwapOrderNo arg = new ArgSwapOrderNo
            {
                SrcId = srcModel.Id,
                SrcOrderNo = srcModel.OrderNo,
                TarId = tarModel.Id,
                TarOrderNo = tarModel.OrderNo
            };
            var ret = GetResult(arg);
            tarModel.OrderNo += srcModel.OrderNo;
            srcModel.OrderNo = tarModel.OrderNo - srcModel.OrderNo;
            tarModel.OrderNo = tarModel.OrderNo - srcModel.OrderNo;
            return ret;
        }

        public ApiResult SwapOrderNo(ArgSwapOrderNo arg)
        {
            return GetResult(arg);
        }


        #endregion

        #region 角色菜单

        public ApiResultList<Sys_Menu> GetMenuTree()
        {
            return GetResultList<Sys_Menu>();
        }

        public ApiResult<string> AddRoleMenu(Sys_RoleMenu model)
        {
            return GetResult<string>(model);
        }

        public ApiResult<string> EditRoleMenu(Sys_RoleMenu model)
        {
            return GetResult<string>(model);
        }

        public ApiResult DelRoleMenu(Sys_RoleMenu model)
        {
            return GetResult<string>(model);
        }

        ///// <summary>
        ///// 根据父级Id获取所有子菜单
        ///// </summary>
        //public List<Sys_Role_Menu> GetRoleMenus(string roleId, string pId, string searchVal, bool isToorBar = false)
        //{
        //    ArgGetRoleMenus arg = new ArgGetRoleMenus
        //    {
        //        RoleId = roleId,
        //        PId = pId,
        //        SearchVal = searchVal,
        //        IsToorBar = isToorBar
        //    };
        //    return GetList<Sys_Role_Menu>(arg);
        //}
        public ApiResultList<Sys_RoleMenu> GetRoleMenus(ArgGetRoleMenus arg)
        {
            return GetResultList<Sys_RoleMenu>(arg);
        }

        #endregion

        public ApiResultList<Sys_RoleMenu> GetMenuByRole()
        {
            return GetResultList<Sys_RoleMenu>();
        }

        /// <summary>
        /// 修改菜单的排序号
        /// </summary>
        public ApiResult SetRoleMenuOrder(string id, int orderNo)
        {
            return GetResult(new { id, orderNo });
        }
        /// <summary>
        /// 修改菜单工具栏的排序号
        /// </summary>
        public ApiResult SetRoleToolMenuOrder(string id, int orderNo)
        {
            return GetResult(new { id, orderNo });
        }

        public ApiResult SetOrderNo(string id, int orderNo)
        {
            return GetResult(new { id, orderNo });
        }

        public ApiResult SetParent(string srcId, string tarPId, int orderNo)
        {
            return GetResult(new { srcId, tarPId, orderNo });
        }
    }
}